window.requestFileSystem  = window.requestFileSystem || window.webkitRequestFileSystem;
BlobBuilder = window.MozBlobBuilder || window.WebKitBlobBuilder || window.BlobBuilder;
var fileSystem;
var fileName;
var type;
var done;
function startFS(f, t, d){
	fileName = f;
	type = t;
	done = d;
	window.requestFileSystem(window.PERSISTENT, 5*1024*1024 /* 5MB */, onInitFs, errorHandler);
}
function download(fs,fileName, type, done){
	
var xhr = new XMLHttpRequest();
xhr.open('GET', fileName, true);
xhr.responseType = 'arraybuffer';
//xhr.onprogress= updateProgress;
xhr.onload = function(e) {
  if (this.status == 200) {
    var bb = new BlobBuilder();
    bb.append(this.response); // Note: not xhr.responseText
    var blob = bb.getBlob(type);
    writeFile(blob, fileName, done);
  }
};

xhr.send();
}
function onInitFs(fs) {
	  console.log('Opened file system: ' + fs.name);
	  fileSystem = fs;
	  download(fs, fileName, type, done);
}

function removeFile(fileName){
	fileSystem.root.getFile(fileName, {create: false}, function(fileEntry) {

	    fileEntry.remove(function() {
	      console.log('File removed.');
	    }, errorHandler);

	  }, errorHandler);
}

function readFile(fileName){
	fileSystem.root.getFile(fileName, {create: false}, function(fileEntry) {
		document.getElementById('fileRef').href = fileEntry.toURL();
		document.getElementById('testing').innerHTML='<embed width="900" src="filesystem:http://localhost:8080/persistent/log.pdf" height="800" id="pdf_content">';
		console.log(fileEntry.toURL());
	}, errorHandler);
	
	
}

function getFileUrl(fileName, done){
	fileSystem.root.getFile(fileName, {create: false}, function(fileEntry) {
		done(fileEntry.toURL());
	}, errorHandler);
}

function writeFile(blobData, fileName, done){
	fileSystem.root.getFile(fileName, {create: true}, function(fileEntry) {
    	
    	// Create a FileWriter object for our FileEntry (log.txt).
	    fileEntry.createWriter(function(fileWriter) {

	      fileWriter.onwriteend = function(e) {
	        console.log('Write completed.');
	        done();
	      };

	      fileWriter.onerror = function(e) {
	        console.log('Write failed: ' + e.toString());
	      };

	      // Create a new Blob and write it to log.txt.
	      console.log('writing');
	      fileWriter.write(blobData);
	      

	    }, errorHandler);
    	
    }, errorHandler);
}
  
    
    
function errorHandler(e) {
	  var msg = '';

	  switch (e.code) {
	    case FileError.QUOTA_EXCEEDED_ERR:
	      msg = 'QUOTA_EXCEEDED_ERR';
	      break;
	    case FileError.NOT_FOUND_ERR:
	      msg = 'NOT_FOUND_ERR';
	      break;
	    case FileError.SECURITY_ERR:
	      msg = 'SECURITY_ERR';
	      break;
	    case FileError.INVALID_MODIFICATION_ERR:
	      msg = 'INVALID_MODIFICATION_ERR';
	      break;
	    case FileError.INVALID_STATE_ERR:
	      msg = 'INVALID_STATE_ERR';
	      break;
	    default:
	      msg = 'Unknown Error';
	      break;
	  };

	  console.log('Error: ' + msg);
	}


	

	
